<?php $this->load->view('header'); ?>
<div class="row-bids">
	<div class="container no-padding">
		<div class="row">
			<div class="col-lg-12 col-md-12">
				<div class="title-invite-freelancer">Invite</div>
			</div>

			<div class="col-lg-12 col-md-12 no-padding space-margin-bottom">
				<div class="col-lg-3 col-md-3">
					<div class="item-invite-freelancer">Freelancer</div>
				</div>
				<div class="col-lg-9 col-md-9 no-padding">
					<div class="col-lg-2 col-md-2">
						<img class="img-responsive" src="<?php if ($freelancer_info->row()->logo != '') echo uimage_url($freelancer_info->row()->logo); else echo image_default(); ?>">	
					</div>
					<div class="col-lg-10 col-md-10">
					<?php
						if(isset($freelancer_info)){
					?>
						<div class="name-invite-freelancer"><?php echo $freelancer_info->row()->user_name;?></div>
						<div><?php echo getCategoryLinks($freelancer_info->row()->user_categories);?></div>
					<?php
						}
					?>
						
					</div>
				</div>
			</div>
			<form action="/" method="POST" id ="form-invite-freelancer">
				<div class="col-lg-12 col-md-12 no-padding space-margin-bottom">
					<div class="col-lg-3 col-md-3">
						<div class = 'item-invite-freelancer'>Message</div>
					</div>
					<div class="col-lg-9 col-md-9">
						<textarea  class="form-control border-invite-freelancer" name="msg-invite-freelancer"  id="msg-invite-freelancer" rows='8'>Hello!&#10;I'd like to invite you to apply to my job.Please review the job post and apply if you're available.&#10;<?php echo $this->loggedInUser->user_name;?>
						</textarea >
					</div>
				</div>

				<div class="col-lg-12 col-md-12 space-margin-bottom">
					<div class="col-lg-3 col-md-3 no-padding">
						<div class = 'item-invite-freelancer'>Choose a Job</div>
					</div>
					<div class="col-lg-9 col-md-9 no-padding">
						<div class="col-lg-3 col-md-3 no-padding" style="left:5px">
							<div class="radio">
								<label class="text-family">
									<input type="radio" name="radio-choose-job" checked="checked">
									Choose exists job
								</label>
							</div> 
						</div>
						<div class="col-lg-9 col-md-9">
							<select class="form-control" id="select-project" name="select-project">
							<?php
								if(isset($projects)){
									foreach ($projects->result() as $value) {
							?>
								<option value="<?php echo $value->id;?>" data-des="<?php echo stripslashes(addslashes($value->description));?>"><?php echo $value->project_name;?></option>
							<?php			
									}
								}else{
							?>
								<option value="0">Bạn chưa có project</option>
							<?php
								}
								
							?>
							</select>
							<?php echo form_error('project_id')?>
						</div>
						<div class="col-lg-12 no-padding" style="left:7px">
							<button type="button" class="btn btn-md btn-success" id="btn-create-new-project">
								Create a new job post
							</button>
							<p class="loading"></p>
						</div>
					</div>
				</div>

				<div class="col-lg-12 col-md-12 no-padding space-margin-bottom">
					<div class="col-lg-3 col-md-3">
						<div class = 'item-invite-freelancer'>Job Description</div>
					</div>
					<div class="col-lg-9 col-md-9">
						<div class="col-lg-12 col-md-12 no-padding text-description" id="show-des-project">
						<?php
							if(isset($projects))
								echo $projects->result()[0]->description;
						?>
						</div>
						
					</div>
				</div>

				<div class="col-lg-12 col-md-12 no-padding space-margin-bottom">
						<div class="col-md-4 col-lg-4 col-lg-offset-3 col-md-offset-3">
							<button type="submit" class="btn btn-md btn-success" name="inviteProgram" value="1">Send Invitation</button>
							<label><a href="#" class="link-cancel">Cancel</a></label>
						</div>
				</div>
			</form>
			
			<div class="col-lg-12 col-md-12 no-padding space-margin-bottom">
				<div class="col-lg-4 col-md-4 col-lg-offset-3 col-md-offset-3">
					<label class="text-family">Already know this Freelancer ?</label><a href="#"> Hire now</a>
				</div>
			</div>


		</div>
	</div>
</div>

<script type="text/javascript">
	$(document).ready(function(){
		// xử lý sự kiện thay đổi project để hiển thị description
		$('#select-project').change(function(){
			var description = $('#select-project option:selected').attr('data-des');
			$("#show-des-project").html(description);
		});
		// xử lý sự kiện click vào nút create a new job post
		$("#btn-create-new-project").on('click',function(e){
			e.preventDefault();
			var current_url = $(location).attr('href');
			$(location).attr('href',"<?php echo site_url('project/create?backUrl="+current_url+"')?>");
		})

		// xử lý sự kiện click vào nút send Invitation
		$("#form-invite-freelancer").validate({
			errorElement: "span", // contain the error msg in a span tag
            errorClass: 'error',
            errorPlacement: function (error, element) { // render error placement for each input type
                if (element.attr("type") == "radio" || element.attr("type") == "checkbox") { // for chosen elements, need to insert the error after the chosen container
                    error.insertAfter($(element).closest('.form-group').children('div').children().last());
                } else if (element.attr("name") == "dd" || element.attr("name") == "mm" || element.attr("name") == "yyyy") {
                    error.insertAfter($(element).closest('.form-group').children('div'));
                } else {
                    error.insertAfter(element);
                    // for other inputs, just perform default behavior
                }
            },
            highlight: function (element) {
                $(element).closest('.help-block').removeClass('valid');
                // display OK icon
                $(element).closest('.form-group').removeClass('has-success').addClass('has-error').find('.symbol').removeClass('ok').addClass('required');
                // add the Bootstrap error class to the control group
            },
            unhighlight: function (element) { // revert the change done by hightlight
                $(element).closest('.form-group').removeClass('has-error');
                // set error class to the control group
            },
            rules:{
                "select-project" : {
                    required : true
                },
                "msg-invite-freelancer" : {
                    required : true
                },
            },
            messages:{
                "select-project":{
                    required : "<?php echo $this->lang->line('project_id_valid')?>"
                },
                "msg-invite-freelancer":{
                    required : "<?php echo $this->lang->line('message_valid')?>"
                },
            },
            submitHandler: function (form) {
            	var url = "<?php echo site_url('userList/inviteProgrammerUpdate'); ?>";
				var receiver_id = <?php echo $freelancer_info->row()->id; ?>;
				var project_id = $("#select-project").val();
				var message = $("#msg-invite-freelancer").val();
				var formData = {
					'receiver_id' 	: <?php echo $freelancer_info->row()->id; ?>,
					'project_id'	: $("#select-project").val(),
					'message'		: $("#msg-invite-freelancer").val()
				};
				$.ajax({
					url : url,
					type : "POST",
					data: formData,
					beforeSend: function() {
	     		    	$('.loading').css("text-align", "center").html('<img src="<?php echo image_url("loading.gif")?>"/>');
	     		    },
					success: function(data){
						$('.loading').empty();
						var res = $.parseJSON(data);
						if(res.status == "COMPLETED"){
	     		    		$('.loading').addClass('success').html("<?php echo $this->lang->line('review_success')?>");
	     		    		setTimeout(5000);
	     		    		$(location).attr('href',"<?php echo site_url('programmer/viewProfile/"+receiver_id+"?notice_invite="+res.msg+"')?>");
		     		    }else if(res.status == "ERROR"){
		     		    	$('.loading').addClass('error').html(res.msg);
			     		}
					},
				})// end ajax
	        }
		})
		
		
	})
</script>
<?php $this->load->view('footer'); ?>